package com.kfm.hotel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.kfm.hotel.domain.RoleMenuDTO;
import com.kfm.hotel.domain.SysRole;
import com.kfm.hotel.domain.SysUserRole;
import com.kfm.hotel.service.SysRoleService;
import com.kfm.hotel.service.SysUserRoleService;
import com.kfm.hotel.util.R;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@RestController
@RequestMapping("/sysrole")
public class SysRoleController {

    @Autowired
    private SysRoleService service;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    @GetMapping
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("sysrole/index");
        return modelAndView;
    }

    @GetMapping("/list")
    @ApiResponse(code = 200, message = "success", response = R.class)
    public R list(@ApiParam(value = "分页参数-页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
                  @ApiParam(value = "分页参数-页大小", defaultValue = "10")@RequestParam(defaultValue = "10") Integer limit,
                  @ApiParam(value = "查询条件") SysRole role) {
        PageInfo<SysRole> pageInfo = service.list(role, page, limit);
        return R.ok(pageInfo);
    }

    @PostMapping("/grantMenu")
    public R grantMenu(@RequestBody RoleMenuDTO roleMenuDTO){
        service.setRoleMenu(roleMenuDTO);
        return R.ok("设置成功");
    }


    @RequestMapping("/grant")
    public R list(){
        List<SysRole> list = service.list();
        return R.ok(list);
    }

    @RequestMapping("/grant/{userId}")
    public R getAllByUserId(@PathVariable("userId") Integer userId){
        QueryWrapper<SysUserRole> wrapper = new QueryWrapper<>();
        wrapper.select("role_id");
        wrapper.eq("user_id", userId);

        List<Object> list = sysUserRoleService.listObjs(wrapper);
        return R.ok(list);
    }
}
